<?php

namespace App\Http\Controllers\oem;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Http\JsonResponse;

class BaseController
{
    protected function getPerPage(): int
    {
        $count = request()->input('per_page', 50);
        if ($count >= 500) {
            $count = 500;
        }

        return $count;
    }

    protected function success($data = [], string $message = '', int $code = 200): JsonResponse
    {
        return response()->json([
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ], $code);
    }

    protected function error(string $message, $data = [], int $code = 400): JsonResponse
    {
        return response()->json([
            'code' => $code,
            'message' => $message,
            'data' => $data,
        ], $code);
    }

    protected function paginated(LengthAwarePaginator $paginator)
    {
        return [
            'per_page' => $paginator->perPage(),
            'total' => $paginator->total(),
            'current_page' => $paginator->currentPage(),
            'items' => $paginator->items(),
        ];
    }
}
